﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Messenger.DataAccess.Attributes
{
    /// <summary>
    /// Number validation
    /// Created by: Dinh Quoc Vuong
    /// Created date: Oct 6th 2011
    /// </summary>
    public class NumberRangeValidationAttribute : ValidationBaseAttribute
    {
        #region fields
        /// <summary>
        /// Data will be stored to check null or not
        /// </summary>
        private object _data = null;
        #endregion

        #region Properties
        /// <summary>
        /// Minumum number
        /// </summary>
        public int MinNumber { get; set; }
        /// <summary>
        /// Maximum number
        /// </summary>
        public int MaxNumber { get; set; }
        #endregion

        #region constuctors
        public NumberRangeValidationAttribute()
            : base()
        {
            //set default number
            this.MinNumber = int.MinValue;
            //set default number
            this.MaxNumber = int.MinValue;
        }
        #endregion

        #region overrides
        /// <summary>
        /// Validate text length
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Validate(object obj)
        {
            _data = obj;
            bool isValid = false;
            if (obj != null)
            {
                int value = (int)obj;
                if (isValid)
                {
                    if (MinNumber != int.MinValue && MaxNumber != int.MinValue)
                    {
                        isValid = value >= MinNumber && value <= MaxNumber;
                    }
                    else if (MinNumber != int.MinValue)
                    {
                        isValid = value >= MinNumber;
                    }
                    else if (MaxNumber != int.MinValue)
                    {
                        isValid = value <= MaxNumber;
                    }
                }
            }
            return isValid;
        }

        /// <summary>
        /// Get error message
        /// </summary>
        /// <param name="propertyName">your property name</param>
        /// <returns></returns>
        public override string GetErrorMessage(string propertyName = null)
        {
            string msg = Message;
            if (_data == null)
            {
                msg = "Property " + propertyName + " is null.";
            }
            else
            {
                if (msg == null)
                {
                    msg = "Property " + propertyName + " is not a valid number. ";
                    if (MinNumber != int.MinValue && MaxNumber != int.MinValue)
                    {
                        msg += string.Format("Number must be in range ({0} - {1}).", MinNumber, MaxNumber);
                    }
                    else if (MinNumber != int.MinValue)
                    {
                        msg += string.Format("Number must be greater than {0}.", MinNumber);
                    }
                    else if (MaxNumber != int.MinValue)
                    {
                        msg += string.Format("Number must be less than {0}.", MaxNumber);
                    }
                }
            }

            return msg;
        }
        #endregion
    }
}
